<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initPlugins() {
        $controller = Zend_Controller_Front::getInstance();
        $registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
        Zend_Registry::setInstance($registry);

        // Add custom routes for detection and mapping
        $router = $controller->getRouter();
        self::_assembleRoutes($router);
        $controller->setParam('registry', $registry);
        $response = new Zend_Controller_Response_Http;
        $response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);
        $controller->setResponse($response);
    }

    protected function _initView() {
        $this->bootstrap('layout');
        $view = $this->getResource('layout')->getView();
        $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
        ZendX_JQuery::enableView($view);
        $view->jQuery()->enable()->uienable();
    }

    protected function _initActionHelpers() {
        $this->bootstrap('frontController');
        $form = Zend_Controller_Action_HelperBroker::getStaticHelper('Form');
        Zend_Controller_Action_HelperBroker::addHelper($form);
    }

    protected static function _assembleRoutes($router) {
        // ArtistController routes
        $route = new Zend_Controller_Router_Route_Regex(
                        'artist/search/([0-9a-zäüöéè\._!;,\s\+\-%]+)',
                        array(
                            'module' => 'default',
                            'controller' => 'artist',
                            'action' => 'search'
                        ),
                        array(
                            'artist' => 1
                        ),
                        'artist/search/%s/page/2'
        );
        $router->addRoute('defaultArtistSearch', $route);

        $route = new Zend_Controller_Router_Route_Regex(
                        'artist/search/([0-9a-zäüöéè\._!;,\s\+\-%]+)/page+/(\d+)',
                        array(
                            'module' => 'default',
                            'controller' => 'artist',
                            'action' => 'search'
                        ),
                        array(
                            'artist' => 1,
                            'page' => 2
                        ),
                        'artist/search/%s/page/%s'
        );
        $router->addRoute('artistSearch', $route);

        $route = new Zend_Controller_Router_Route_Regex(
                        'artist/((?!validate)[0-9a-zäüöéè\._!;&\(\),\s\+\-%]+)',
                        array(
                            'module' => 'default',
                            'controller' => 'artist',
                            'action' => 'show'
                        ),
                        array(
                            'artist' => 1
                        ),
                        'artist/%s'
        );
        $router->addRoute('artistShow', $route);

        // VenueController routes
        $route = new Zend_Controller_Router_Route_Regex(
                        'venue/search/([0-9a-zäüöéè\._!;,\s\+\-%]+)',
                        array(
                            'module' => 'default',
                            'controller' => 'venue',
                            'action' => 'search'
                        ),
                        array(
                            'venue' => 1
                        ),
                        'venue/search/%s/page/2'
        );
        $router->addRoute('defaultVenueSearch', $route);

        $route = new Zend_Controller_Router_Route_Regex(
                        'venue/search/([0-9a-zäüöéè\._!;,\s\+\-%]+)/page+/(\d+)',
                        array(
                            'module' => 'default',
                            'controller' => 'venue',
                            'action' => 'search'
                        ),
                        array(
                            'venue' => 1,
                            'page' => 2
                        ),
                        'venue/search/%s/page/%s'
        );
        $router->addRoute('venueSearch', $route);

        $route = new Zend_Controller_Router_Route_Regex(
                        'venue/search/([0-9a-zäüöéè\._!;,\s\+\-%]+)/([a-z]{2})',
                        array(
                            'module' => 'default',
                            'controller' => 'venue',
                            'action' => 'search'
                        ),
                        array(
                            'venue' => 1,
                            'country' => 2
                        ),
                        'venue/search/%s/%s/page/2'
        );
        $router->addRoute('defaultVenueCountrySearch', $route);

        $route = new Zend_Controller_Router_Route_Regex(
                        'venue/search/([0-9a-zäüöéè\._!;,\s\+\-%]+)/([a-z]{2})/page+/(\d+)',
                        array(
                            'module' => 'default',
                            'controller' => 'venue',
                            'action' => 'search'
                        ),
                        array(
                            'venue' => 1,
                            'country' => 2,
                            'page' => 3
                        ),
                        'venue/search/%s/%s/page/%s'
        );
        $router->addRoute('venueCountrySearch', $route);

        $route = new Zend_Controller_Router_Route_Regex(
                        'venue/([0-9]+)/([0-9a-zäüöéè\._!;&\(\),\s\+\-%]+)',
                        array(
                            'module' => 'default',
                            'controller' => 'venue',
                            'action' => 'show'
                        ),
                        array(
                            'id' => 1,
                            'venue' => 2
                        ),
                        'venue/%s/%s/'
        );
        $router->addRoute('venueShow', $route);

        // LocationController routes
        $route = new Zend_Controller_Router_Route_Regex(
                        'location/((?!validate)[0-9a-zäüöéè\._!;&\(\),\s\+\-%]+)',
                        array(
                            'module' => 'default',
                            'controller' => 'location',
                            'action' => 'show'
                        ),
                        array(
                            'location' => 1
                        ),
                        'location/%s'
        );
        $router->addRoute('locationDefaultShow', $route);

        $route = new Zend_Controller_Router_Route_Regex(
                        'location/((?!validate)[0-9a-zäüöéè\._!;&\(\),\s\+\-%]+)/([0-9]+)',
                        array(
                            'module' => 'default',
                            'controller' => 'location',
                            'action' => 'show'
                        ),
                        array(
                            'location' => 1,
                            'within' => 2
                        ),
                        'location/%s/%s'
        );
        $router->addRoute('locationShow', $route);
    }

}

